﻿using System;
using System.Drawing;
using System.Windows.Forms;
namespace Moonlite.UI.Shell.Controls {
    public class BorderPanel : Panel {
        private Color borderColor = SystemColors.ControlDark;
        private Padding borderPadding = Padding.Empty;
        private Sides borderSides = Sides.All;
        private ButtonBorderStyle borderStyle = ButtonBorderStyle.Solid;
        private Color innerColor = SystemColors.Control;
        private bool useInnerColor;
        public Color BorderColor {
            get { return borderColor; }
            set { borderColor = value; }
        }
        public Padding BorderPadding {
            get { return borderPadding; }
            set { borderPadding = value; }
        }
        public Sides BorderSides {
            get { return borderSides; }
            set { borderSides = value; }
        }
        public new ButtonBorderStyle BorderStyle {
            get { return borderStyle; }
            set { borderStyle = value; }
        }
        public Color InnerColor {
            get { return innerColor; }
            set { innerColor = value; }
        }
        public bool UseInnerColor {
            get { return useInnerColor; }
            set { useInnerColor = value; }
        }
        protected override void OnPaintBackground(PaintEventArgs pevent) {
            base.OnPaintBackground(pevent);
            if ((borderSides != Sides.None) || useInnerColor) {
                Rectangle rectangle = new Rectangle(base.ClientRectangle.Left + BorderPadding.Left, base.ClientRectangle.Top + BorderPadding.Top, ((base.ClientRectangle.Width - BorderPadding.Left) - BorderPadding.Right) - 1, ((base.ClientRectangle.Height - BorderPadding.Top) - BorderPadding.Bottom) - 1);
                if (useInnerColor) {
                    using (Brush brush = new SolidBrush(InnerColor)) {
                        pevent.Graphics.FillRectangle(brush, rectangle.Left, rectangle.Top, rectangle.Width + 1, rectangle.Height + 1);
                    }
                }
                if (borderSides != Sides.None) {
                    using (Pen pen = new Pen(BorderColor)) {
                        if ((borderSides & Sides.Top) == Sides.Top) {
                            pevent.Graphics.DrawLine(pen, new Point(rectangle.Left, rectangle.Top), new Point(rectangle.Right, rectangle.Top));
                        }
                        if ((borderSides & Sides.Right) == Sides.Right) {
                            pevent.Graphics.DrawLine(pen, new Point(rectangle.Right, rectangle.Top), new Point(rectangle.Right, rectangle.Bottom));
                        }
                        if ((borderSides & Sides.Bottom) == Sides.Bottom) {
                            pevent.Graphics.DrawLine(pen, new Point(rectangle.Right, rectangle.Bottom), new Point(rectangle.Left, rectangle.Bottom));
                        }
                        if ((borderSides & Sides.Left) == Sides.Left) {
                            pevent.Graphics.DrawLine(pen, new Point(rectangle.Left, rectangle.Bottom), new Point(rectangle.Left, rectangle.Top));
                        }
                    }
                }
            }
        }
        public BorderPanel() {
            base.ResizeRedraw = true;
            borderColor = SystemColors.ControlDark;
        }
    }
}
